<?php

/**
 * finalClothFormItem actions.
 *
 * @package    WacStorehouse
 * @subpackage finalClothFormItem
 * @author     JianBinBi <prince.bi@gmail.com>
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 *
 * notes:
 * if clone as another one, below tags need to be replace to ur target module tag
 * "finalClothForm" , "FinalClothForm", "final_cloth_form"
 */
class finalClothFormItemActions extends WacCommonActions
{
    /**
     * Executes an application defined process prior to execution of this sfAction object.
     *
     * By default, this method is empty.
     */
    public function preExecute() {
        $this->moduleName = sfContext::getInstance()->getRequest()->getParameter("module");
        $moduleName = $this->moduleName;
        $this->mainModuleTable = Doctrine::getTable(WacTable::$$moduleName);

        parent::preExecute();
    }

    /*
   * override filter list
    */
    public function filterList($listObjs) {
        $filterArr = array();
        if(count($listObjs) > 0) {
            foreach($listObjs as $listObj) {
                $tmpArr = $listObj->toArray();
                $tmpArr['production_order_code']        = $listObj->getWacProductionOrder()->getCode();
                $tmpArr['final_cloth_form_code']        = $listObj->getWacFinalClothForm()->getCode();
                $tmpArr['goods_number_unit_code_name']  = $listObj->getWacGoodsNumberUnitCode()->getName();
//                $tmpArr['factory_name']                 = $listObj->getWacFactory()->getName();
                $tmpArr['storehouse_name']              = $listObj->getWacStorehouse()->getName();
                $tmpArr['color_name']                   = $listObj->getWacColor()->getName();
                $tmpArr['jar_name']                     = $listObj->getWacJar()->getName();
                $tmpArr['raw_material_name']            = $listObj->getWacRawMaterial()->getName();
                $tmpArr['goods_category_name']          = $listObj->getWacGoodsCategory()->getName();

                $filterArr[] = $tmpArr;
            }
        }

        return $filterArr;
    }

    /*
   * @return inspect result
    */
    public function inspectDataValidation(sfWebRequest $request, $params=array()) {
        $result    = JsCommonData::getSuccessDatum();
        $reqParams = $request->getParameterHolder()->getAll();

        if(empty($reqParams['goods_category_id']) || !Doctrine::getTable(WacTable::$goodsCategory)->isExistedId($reqParams['goods_category_id'])) {
            $result = JsCommonData::getErrorDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_goods_category"),  WacErrorCode::$logicError
            );
            return $result;
        }

        if(empty($reqParams['final_cloth_form_id']) || !Doctrine::getTable(WacTable::$finalClothForm)->isExistedId($reqParams['final_cloth_form_id'])) {
            $result = JsCommonData::getErrorDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_final_cloth_form"),  WacErrorCode::$logicError
            );
            return $result;
        }

        if(empty($reqParams['color_id']) || !Doctrine::getTable(WacTable::$color)->isExistedId($reqParams['color_id'])) {
            $result = JsCommonData::getErrorDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_color"),  WacErrorCode::$logicError
            );
            return $result;
        }

        if(empty($reqParams['jar_id']) || !Doctrine::getTable(WacTable::$jar)->isExistedId($reqParams['jar_id'])) {
            $result = JsCommonData::getErrorDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_jar"),  WacErrorCode::$logicError
            );
            return $result;
        }

        if(empty($reqParams['raw_material_id']) || !Doctrine::getTable(WacTable::$rawMaterial)->isExistedId($reqParams['raw_material_id'])) {
            $result = JsCommonData::getErrorDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_raw_material"),  WacErrorCode::$logicError
            );
            return $result;
        }

//        if(empty($reqParams['factory_id']) || !Doctrine::getTable(WacTable::$factory)->isExistedId($reqParams['factory_id'])) {
//            $result = JsCommonData::getErrorDatum(
//                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_factory"),  WacErrorCode::$logicError
//            );
//            return $result;
//        }

        return $result;
    }

    /*
   * @return list data array
    */
    public function executeAdd(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $reqParams     = $this->getRequest()->getParameterHolder()->getAll();
        $resultSet     = JsCommonData::getCommonDatum();
        $inspectResult = $this->inspectDataValidation($request);

        if($inspectResult['status'] == WacOperationStatus::$Error) {
            $resultSet['info'] = $inspectResult;
        }
        else {
            $objFinalClothForm = Doctrine::getTable(WacTable::$finalClothForm)->findOneBy('id', $reqParams['final_cloth_form_id']);
            $targetItem      = $this->mainModuleTable->create();

            if(count($reqParams)>0) {
//              $targetItem->set($reqParams['']);

                $targetItem->setProductionOrderId($objFinalClothForm->getProductionOrderId());
                $targetItem->setSrcProductionOrderId($objFinalClothForm->getProductionOrderId());
                $targetItem->setFinalClothFormId($objFinalClothForm->getId());
                $targetItem->setSupplierId($objFinalClothForm->getSupplierId());
                $targetItem->setFactoryId($objFinalClothForm->getFactoryId());
                $targetItem->setStorehouseId($objFinalClothForm->getStorehouseId());

                if(isset($reqParams['goods_category_id'])) {
                    $targetItem->setGoodsCategoryId($reqParams['goods_category_id']);
                }
                if(isset($reqParams['batch'])) {
                    $targetItem->setBatch($reqParams['batch']);
                }
                if(isset($reqParams['jar_id'])) {
                    $targetItem->setJarId($reqParams['jar_id']);
                }
                if(isset($reqParams['goods_number'])) {
                    $targetItem->setGoodsNumber($reqParams['goods_number']);
                }
                if(isset($reqParams['goods_number_unit_code'])) {
                    $targetItem->setGoodsNumberUnitCode($reqParams['goods_number_unit_code']);
                }
                if(isset($reqParams['goods_number_unit_price'])) {
                    $targetItem->setGoodsNumberUnitPrice($reqParams['goods_number_unit_price']);
                }
                if(isset($reqParams['color_id'])) {
                    $targetItem->setColorId($reqParams['color_id']);
                }
                if(isset($reqParams['raw_material_id'])) {
                    $targetItem->setRawMaterialId($reqParams['raw_material_id']);
                }
                
                if(isset($reqParams['processing_fee'])) {
                    $targetItem->setProcessingFee($reqParams['processing_fee']);
                }

                if(isset($reqParams['memo'])) {
                    $targetItem->setMemo($reqParams['memo']);
                }

                $targetItem->save();

                $this->afterAdd($request, $targetItem);


            }

            $resultSet['items'][0] = $targetItem->toArray();
            $resultSet['info']     = JsCommonData::getSuccessDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_add_succ")
            );
        }

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);

    }

    /*
   * @return list data array
    */
    public function executeEdit(sfWebRequest $request) {
        // forward to 404 if no id
        $this->forward404Unless($request->hasParameter('id'));

        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $reqParams     = $this->getRequest()->getParameterHolder()->getAll();
        $resultSet     = JsCommonData::getCommonDatum();
        $inspectResult = $this->inspectDataValidation($request);

        if($inspectResult['status'] == WacOperationStatus::$Error) {
            $resultSet['info'] = $inspectResult;
        }
        else {
            $targetItem = $this->mainModuleTable->findOneById($request->getParameter('id'));

            if(count($reqParams)>0) {
                if(isset($reqParams['goods_category_id'])) {
                    $targetItem->setGoodsCategoryId($reqParams['goods_category_id']);
                }
                if(isset($reqParams['batch'])) {
                    $targetItem->setBatch($reqParams['batch']);
                }
                if(isset($reqParams['jar_id'])) {
                    $targetItem->setJarId($reqParams['jar_id']);
                }
                if(isset($reqParams['goods_number'])) {
                    $targetItem->setGoodsNumber($reqParams['goods_number']);
                }
                if(isset($reqParams['goods_number_unit_code'])) {
                    $targetItem->setGoodsNumberUnitCode($reqParams['goods_number_unit_code']);
                }
                if(isset($reqParams['goods_number_unit_price'])) {
                    $targetItem->setGoodsNumberUnitPrice($reqParams['goods_number_unit_price']);
                }
                if(isset($reqParams['color_id'])) {
                    $targetItem->setColorId($reqParams['color_id']);
                }
                if(isset($reqParams['raw_material_id'])) {
                    $targetItem->setRawMaterialId($reqParams['raw_material_id']);
                }
                if(isset($reqParams['processing_fee'])) {
                    $targetItem->setProcessingFee($reqParams['processing_fee']);
                }

                if(isset($reqParams['memo'])) {
                    $targetItem->setMemo($reqParams['memo']);
                }

                $targetItem->save();

                $this->afterEdit($request, $targetItem);

            }

            $resultSet['items'][0] = $targetItem->toArray();
            $resultSet['info']     = JsCommonData::getSuccessDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_edit_succ")
            );
        }

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);

    }

    /*
     * @return list data array
     */
    public function executeGetFormData(sfWebRequest $request) {
        // forward to 404 if no id
        $this->forward404Unless($request->hasParameter('final_cloth_form_id'));

        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = JsCommonData::getCommonDatum();
        $resultSet['info'] = JsCommonData::getSuccessDatum();

        $objFinalClothForm = Doctrine::getTable(WacTable::$finalClothForm)->findOneBy('id', $request->getParameter('final_cloth_form_id'));

        $resultSet['items']['unit'] = Doctrine::getTable(WacTable::$unit)->getCodeNameHash();
        $resultSet['items']['raw_material'] = Doctrine::getTable(WacTable::$rawMaterial)->getIdNameHash();
        $resultSet['items']['default']['goods_number_unit_code'] = Doctrine::getTable(WacTable::$systemParameter)->getValueByCode(WacAppSettingCode::$conttonUnitCode);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);

    }
}